package net.bxx2004.pandalib.bukkit.particle.application;

import net.bxx2004.pandalib.bukkit.particle.Particle;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import xyz.xenondevs.particle.ParticleBuilder;
import xyz.xenondevs.particle.ParticleEffect;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;

public class ImageParticle implements Particle {
    private File file;
    private BufferedImage image;
    int width = 0;
    int height = 0;
    public ImageParticle(String path){
        this.file = new File(path);
        try {
            image = ImageIO.read(file);
        }catch (Exception e){e.printStackTrace();}
        this.width = image.getWidth();
        this.height = image.getHeight();
    }
    private boolean isSpawn(int r, int g, int b){
        int grary = (int) (0.299 * r + 0.587 * g + 0.114 *b);
        if (grary < 132){
            return true;
        }
        return false;
    }
    @Override
    public void show(Location target) {
        for (int i = image.getMinX(); i < width; i++) {
            for (int j = image.getMinY(); j < height; j++) {
                int pixel = image.getRGB(i, j);
                int r  = (pixel & 0xff0000) >> 16;
                int g  = (pixel & 0xff00) >> 8;
                int b  = (pixel & 0xff);
                double xi = target.getX() + i/ 8;
                double yi = target.getY() + i/ 8;
                if (isSpawn(r,g,b)){
                    basic(r,g,b).setLocation(target.clone().add(xi/8,yi/8,0)).display(Bukkit.getPlayer("ZZZiChuan"));
                }
            }
        }
    }

    @Override
    public ParticleBuilder basic(int r, int g, int b) {
        ParticleBuilder builder = new ParticleBuilder(ParticleEffect.REDSTONE);
        return builder;
    }
}
